import pandas as pd
import ast
import os
from config import DATA_DIR
from sklearn.preprocessing import MinMaxScaler

data_path=os.path.join(DATA_DIR,'processed','hurun_rich_list_cleaned.csv')
df=pd.read_csv(data_path)
# 将csv中的字符串列表转换为pytohn中的列表对象
df['industry']=df['industry'].apply(lambda x:ast.literal_eval(x) if pd.notnull(x) else [])
df['school'] = df['school'].apply(lambda x: ast.literal_eval(x) if isinstance(x, str) else [])



print('开始分析数据......')

def gender_analysis():
    known_gender = df[df['gender'].isin(['先生', '女士'])]
    gender_dist = known_gender['gender'].value_counts().reset_index()
    gender_dist.columns = ['性别', '人数']
    return gender_dist

def age_analysis():
    age_bins = [20, 30, 40, 50, 60, 70, 80, 90, 100]
    age_labels = ['20-29', '30-39', '40-49', '50-59', '60-69', '70-79', '80-89', '90+']
    age_dist = pd.cut(
        df['age'].dropna(),
        bins=age_bins,
        labels=age_labels
    ).value_counts().sort_index().reset_index()
    age_dist.columns=['年龄','人数']
    return age_dist

def province_analysis():
    df['省份'] = df['birthplace'].apply(lambda x: x.split('-')[0] if '-' in str(x) else x)
    province_dist = df['省份'].value_counts().reset_index()
    province_dist.columns=['省份','人数']
    return province_dist

def education_analysis():
    education_dist = df['education'].value_counts().reset_index()
    education_dist.columns = ['学历', '人数']
    return education_dist

def school_analysis():
    school_data = []
    for _, row in df.iterrows():
        for school in row['school']:
            school_data.append({'毕业院校': school})
    school_df = pd.DataFrame(school_data)
    school_dist = school_df['毕业院校'].value_counts().reset_index()
    school_dist.columns=['毕业院校','人数']
    return school_dist

def unify_similar_industries(industry_series):
    """将连续且前两个字符相同的行业名称统一为第一个出现的名称"""
    unified = industry_series.copy()
    i = 0
    n = len(unified)
    while i < n:
        current_prefix = unified.iloc[i][:2]
        j = i + 1
        while j < n and unified.iloc[j][:2] == current_prefix:
            j += 1
        if j > i + 1:
            unified.iloc[i:j] = unified.iloc[i]
        i = j
    return unified

def industry_analysis_base():
    industry_data = []
    for _, row in df.iterrows():
        industries = row['industry']
        wealth = row['wealth']
        ranking_change=row['ranking_change']
        wealth_change=row['wealth_change']
        if industries: 
            # 将财富、变化值平分到每个行业
            per_industry_ranking_change=ranking_change / len(industries)
            per_industry_wealth_change = wealth_change / len(industries)
            per_industry_wealth = wealth / len(industries)
            for industry in industries:
                industry_data.append({
                    '排名变化':per_industry_ranking_change,
                    '财富值变化':per_industry_wealth_change,
                    '行业': industry,
                    '财富值': per_industry_wealth,
                })
    industry_df = pd.DataFrame(industry_data) 
    industry_df = industry_df.sort_values(by='行业')
    industry_df['行业'] = unify_similar_industries(industry_df['行业'])
    industry_stats = industry_df.groupby('行业').agg(
        # 规模维度指标
        富豪数量=('财富值', 'count'),
        财富平均值=('财富值', 'mean'),
        总财富值=('财富值','sum'),
        财富中位数=('财富值', 'median'),
        # 垄断程度维度指标
        财富标准差=('财富值', 'std'),
        财富分位差=('财富值', lambda x: x.quantile(0.75) - x.quantile(0.25)),  # 新增分位差
        HHI指数=('财富值', lambda x: ((x / x.sum())**2).sum()),
        # 稳定性维度指标
        财富变化标准差=('财富值变化', 'std'),
        排名变化标准差=('排名变化', 'std'),
        # 增长潜力维度指标
        平均财富变化=('财富值变化', 'mean'),
        财富正向变化占比=('财富值变化', lambda x: (x > 0).mean()),
        排名上升占比=('排名变化', lambda x: (x > 0).mean()),
    ).reset_index()
    MIN_SAMPLE = 7  # 至少5个富豪的行业才保留
    industry_stats = industry_stats[industry_stats['富豪数量'] >= MIN_SAMPLE]

    return industry_stats

def industry_analysis_factor(industry_stats,metrics_weights,analysis_name):
    print(f'正在分析{analysis_name}......')
    metrics = list(metrics_weights.keys())
    scale_metrics = industry_stats[metrics].copy()
    # MinMax标准化（0-1范围）
    minmax_scaler = MinMaxScaler()
    scaled_minmax = minmax_scaler.fit_transform(scale_metrics)
    scaled_cols = [f'{col}_标准' for col in metrics]
    industry_stats[scaled_cols] = scaled_minmax
    # 新权重分配（考虑移除总财富值后的调整）
    weighted_sum = 0
    for col, weight in metrics_weights.items():
        weighted_sum += industry_stats[f'{col}_标准'] * weight
    industry_stats[analysis_name] = weighted_sum
    print('行业规模分析完成！')
    return industry_stats

def industry_analysis():
    industry_stats=industry_analysis_base()
    weights = {
        '富豪数量': 0.3,
        '总财富值': 0.4, 
        '财富平均值': 0.2,
        '财富中位数': 0.1
    }
    industry_stats=industry_analysis_factor(industry_stats,weights,'行业规模')
    weights = {
        '财富标准差': 0.3,   # 反映整体离散程度
        '财富分位差': 0.4,   # 捕捉极端差距（如P90/P10）
        'HHI指数': 0.3       # 直接衡量市场集中度
    }
    industry_stats=industry_analysis_factor(industry_stats,weights,'垄断程度')
    weights = {
        '财富变化标准差': 0.5,   # 核心波动指标（财富变化的离散程度）
        '排名变化标准差': 0.5    # 排名稳定性同样重要（如行业洗牌频率） 
    }
    industry_stats=industry_analysis_factor(industry_stats,weights,'稳定性')
    weights = {
        '平均财富变化': 0.4,          # 绝对增长水平
        '财富正向变化占比': 0.4,      # 行业整体向上趋势的普遍性
        '排名上升占比': 0.2           # 相对排名进步（辅助指标）
    }
    industry_stats=industry_analysis_factor(industry_stats,weights,'发展潜力')
    weights = {
        '行业规模': 0.2,          # 绝对增长水平
        '垄断程度': 0.25,      # 行业整体向上趋势的普遍性
        '稳定性': 0.25,           # 相对排名进步（辅助指标）
        '发展潜力':0.3
    }
    industry_stats=industry_analysis_factor(industry_stats,weights,'综合发展态势')
    return industry_stats

# 行业 富豪数量 总财富值 平均财富值
INDUSTRY_STATS = industry_analysis()

# 性别 人数
GENDER_DIST = gender_analysis()

# 年龄 人数 百分比
AGE_DIST= age_analysis()

# 省份 人数
PROVINCE_DIST= province_analysis()

# 学历 人数
EDUCATION_DIST = education_analysis()

# 毕业院校	人数
SCHOOL_DIST = school_analysis()

print("数据分析完成！")